<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>Other Standard C Functions</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">Other Standard C Functions</a>
  </div>

  <div class="name-format">
    abort
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  void abort( void );
</pre>

  <p>The function abort() terminates the current program. Depending on
  the implementation, the return value can indicate failure.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="assert.html">assert</a><br>
    <a href="atexit.html">atexit</a><br>
    <a href="exit.html">exit</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    assert
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;assert.h&gt;
  assert( exp );
</pre>

  <p>The assert() macro is used to test for errors. If <em>exp</em>
  evaluates to zero, assert() writes information to
  <strong>stderr</strong> and exits the program. If the macro NDEBUG is
  defined, the assert() macros will be ignored.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="abort.html">abort</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    atexit
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  int atexit( void (*func)(void) );
</pre>

  <p>The function atexit() causes the function pointed to by
  <em>func</em> to be called when the program terminates. You can make
  multiple calls to atexit() (at least 32, depending on your compiler)
  and those functions will be called in reverse order of their
  establishment. The return value of atexit() is zero upon success, and
  non-zero on failure.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="abort.html">abort</a><br>
    <a href="exit.html">exit</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    bsearch
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
</pre>

  <p>The bsearch() function searches <em>buf[0]</em> to
  <em>buf[num-1]</em> for an item that matches <em>key</em>, using a
  binary search. The function <em>compare</em> should return negative
  if its first argument is less than its second, zero if equal, and
  positive if greater. The items in the array <em>buf</em> should be in
  ascending order. The return value of bsearch() is a pointer to the
  matching item, or <strong>NULL</strong> if none is found.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="qsort.html">qsort</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    exit
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  void exit( int exit_code );
</pre>

  <p>The exit() function stops the program. <em>exit_code</em> is
  passed on to be the return value of the program, where usually zero
  indicates success and non-zero indicates an error.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="abort.html">abort</a><br>
    <a href="atexit.html">atexit</a><br>
    <a href="system.html">system</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    getenv
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  char *getenv( const char *name );
</pre>

  <p>The function getenv() returns environmental information associated
  with <em>name</em>, and is very implementation dependent.
  <strong>NULL</strong> is returned if no information about
  <em>name</em> is available.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="system.html">system</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    longjmp
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;setjmp.h&gt;
  void longjmp( jmp_buf envbuf, int status );
</pre>

  <p>The function longjmp() causes the program to start executing code
  at the point of the last call to <a href="setjmp.html">setjmp</a>().
  <em>envbuf</em> is usually set through a call to <a href=
  "setjmp.html">setjmp</a>(). <em>status</em> becomes the return value
  of <a href="setjmp.html">setjmp</a>() and can be used to figure out
  where longjmp() came from. <em>status</em> should not be set to
  zero.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="setjmp.html">setjmp</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    qsort
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
</pre>

  <p>The qsort() function sorts <em>buf</em> (which contains
  <em>num</em> items, each of size <em>size</em>) using <a href=
  "http://en.wikipedia.org/wiki/Quicksort">Quicksort</a>. The
  <em>compare</em> function is used to compare the items in
  <em>buf</em>. <em>compare</em> should return negative if the first
  argument is less than the second, zero if they are equal, and
  positive if the first argument is greater than the second. qsort()
  sorts <em>buf</em> in ascending order.</p>

  <div class="related-examples-format">
    Example code:
  </div>

  <div class="related-examples">
    <p>For example, the following bit of code uses qsort() to sort an
    array of integers:</p>
    <pre class="example-code">
 int compare_ints( const void* a, const void* b ) {
   int* arg1 = (int*) a;
   int* arg2 = (int*) b;
   if( *arg1 &lt; *arg2 ) return -1;
   else if( *arg1 == *arg2 ) return 0;
   else return 1;
 }              

 int array[] = { -2, 99, 0, -743, 2, 3, 4 };
 int array_size = 7;            

 ...            

 printf( &quot;Before sorting: &quot; );
 for( int i = 0; i &lt; array_size; i++ ) {
   printf( &quot;%d &quot;, array[i] );
 }
 printf( &quot;\n&quot; );              

 qsort( array, array_size, sizeof(int), compare_ints );         

 printf( &quot;After sorting: &quot; );
 for( int i = 0; i &lt; array_size; i++ ) {
   printf( &quot;%d &quot;, array[i] );
 }
 printf( &quot;\n&quot; );              
</pre>

    <p>When run, this code displays the following output:</p>
    <pre class="example-code">
 Before sorting: -2 99 0 -743 2 3 4
 After sorting: -743 -2 0 2 3 4 99              
</pre>
  </div>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="bsearch.html">bsearch</a><br>
    (C++ Algorithms) <a href="../cppalgorithm/sort.html">sort</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    raise
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;signal.h&gt;
  int raise( int signal );
</pre>

  <p>The raise() function sends the specified <em>signal</em> to the
  program. Some signals:</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Signal</th>

      <th class="code-table-th">Meaning</th>
    </tr>

    <tr>
      <td class="code-table-td">SIGABRT</td>

      <td class="code-table-td">Termination error</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGFPE</td>

      <td class="code-table-td">Floating pointer error</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGILL</td>

      <td class="code-table-td">Bad instruction</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGINT</td>

      <td class="code-table-td">User presed CTRL-C</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGSEGV</td>

      <td class="code-table-td">Illegal memory access</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGTERM</td>

      <td class="code-table-td">Terminate program</td>
    </tr>
  </table>

  <p>The return value is zero upon success, nonzero on failure.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="signal.html">signal</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    rand
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  int rand( void );
</pre>

  <p>The function rand() returns a pseudorandom integer between zero
  and RAND_MAX. An example:</p>
  <pre class="example-code">
   srand( time(<strong>NULL</strong>) );
   for( i = 0; i &lt; 10; i++ )
     printf( &quot;Random number #%d: %d\n&quot;, i, rand() );          
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="srand.html">srand</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    setjmp
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;setjmp.h&gt;
  int setjmp( jmp_buf envbuf );
</pre>

  <p>The setjmp() function saves the system stack in <em>envbuf</em>
  for use by a later call to <a href="longjmp.html">longjmp</a>(). When
  you first call setjmp(), its return value is zero. Later, when you
  call <a href="longjmp.html">longjmp</a>(), the second argument of
  <a href="longjmp.html">longjmp</a>() is what the return value of
  setjmp() will be. Confused? Read about <a href=
  "longjmp.html">longjmp</a>().</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="longjmp.html">longjmp</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    signal
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;signal.h&gt;
  void ( *signal( int signal, void (* func) (int)) ) (int);
</pre>

  <p>The signal() function sets <em>func</em> to be called when
  <em>signal</em> is recieved by your program. <em>func</em> can be a
  custom signal handler, or one of these macros (defined in
  signal.h):</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Macro</th>

      <th class="code-table-th">Explanation</th>
    </tr>

    <tr>
      <td class="code-table-td">SIG_DFL</td>

      <td class="code-table-td">default signal handling</td>
    </tr>

    <tr>
      <td class="code-table-td">SIG_IGN</td>

      <td class="code-table-td">ignore the signal</td>
    </tr>
  </table>

  <p>Some basic signals that you can attach a signal handler to
  are:</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Signal</th>

      <th class="code-table-th">Description</th>
    </tr>

    <tr>
      <td class="code-table-td">SIGTERM</td>

      <td class="code-table-td">Generic stop signal that can be
      caught.</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGINT</td>

      <td class="code-table-td">Interrupt program, normally
      ctrl-c.</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGQUIT</td>

      <td class="code-table-td">Interrupt program, similar to
      SIGINT.</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGKILL</td>

      <td class="code-table-td">Stops the program. Cannot be
      caught.</td>
    </tr>

    <tr>
      <td class="code-table-td">SIGHUP</td>

      <td class="code-table-td">Reports a disconnected terminal.</td>
    </tr>
  </table>

  <p>The return value of signal() is the address of the previously
  defined function for this signal, or SIG_ERR is there is an
  error.</p>

  <div class="related-examples-format">
    Example code:
  </div>

  <div class="related-examples">
    <p>The following example uses the signal() function to call an
    arbitrary number of functions when the user aborts the program. The
    functions are stored in a vector, and a single &quot;clean-up&quot;
    function calls each function in that vector of functions when the
    program is aborted:</p>
    <pre class="example-code">
void f1() {
  cout &lt;&lt; &quot;calling f1()...&quot; &lt;&lt; endl;
}               

void f2() {
  cout &lt;&lt; &quot;calling f2()...&quot; &lt;&lt; endl;
}               

typedef void(*endFunc)(void);
vector&lt;endFunc&gt; endFuncs;         

void cleanUp( int dummy ) {
  for( unsigned int i = 0; i &lt; endFuncs.size(); i++ ) {
    endFunc f = endFuncs.at(i);
    (*f)();
  }
  exit(-1);
}               

int main() {            

  // connect various signals to our clean-up function
  signal( SIGTERM, cleanUp );
  signal( SIGINT, cleanUp );
  signal( SIGQUIT, cleanUp );
  signal( SIGHUP, cleanUp );            

  // add two specific clean-up functions to a list of functions
  endFuncs.push_back( f1 );
  endFuncs.push_back( f2 );             

  // loop until the user breaks
  while( 1 );           

  return 0;
}               
</pre>
  </div>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="raise.html">raise</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    srand
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  void srand( unsigned seed );
</pre>

  <p>The function srand() is used to seed the random sequence generated
  by <a href="rand.html">rand</a>(). For any given <em>seed</em>,
  rand() will generate a specific &quot;random&quot; sequence over and
  over again.</p>
  <pre class="example-code">
   srand( time(<strong>NULL</strong>) );
   for( i = 0; i &lt; 10; i++ )
     printf( &quot;Random number #%d: %d\n&quot;, i, rand() );          
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="rand.html">rand</a><br>
    (Standard C Date &amp; Time) <a href=
    "../stddate/time.html">time</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    system
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdlib.h&gt;
  int system( const char *command );
</pre>

  <p>The system() function runs the given <em>command</em> by passing
  it to the default command interpreter.</p>  

  <p>The return value is usually zero if the command executed without
  errors. If <em>command</em> is <strong>NULL</strong>, system() will
  test to see if there is a command interpreter available. Non-zero
  will be returned if there is a command interpreter available, zero
  if not.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="exit.html">exit</a><br>
    <a href="getenv.html">getenv</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    va_arg
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdarg.h&gt;
  type va_arg( <strong>va_list</strong> argptr, type );
  void va_end( <strong>va_list</strong> argptr );
  void va_start( <strong>va_list</strong> argptr, last_parm );
</pre>

  <p>The va_arg() macros are used to pass a variable number of
  arguments to a function.</p>

  <ol>
    <li>First, you must have a call to va_start() passing a valid
    <strong>va_list</strong> and the mandatory first argument of the
    function. This first argument can be anything; one way to use it is
    to have it be an integer describing the number of parameters being
    passed.</li>

    <li>Next, you call va_arg() passing the <strong>va_list</strong>
    and the type of the argument to be returned. The return value of
    va_arg() is the current parameter.</li>

    <li>Repeat calls to va_arg() for however many arguments you
    have.</li>

    <li>Finally, a call to va_end() passing the
    <strong>va_list</strong> is necessary for proper cleanup.</li>
  </ol>

  <p>For example:</p>
  <pre class="example-code">
  int sum( int num, ... ) {
    int answer = 0;
    <strong>va_list</strong> argptr;            

    va_start( argptr, num );            

    for( ; num &gt; 0; num-- ) {
      answer += va_arg( argptr, int );
    }           

    va_end( argptr );           

    return( answer );
  }             
                

  int main( void ) {            

    int answer = sum( 4, 4, 3, 2, 1 );
    printf( &quot;The answer is %d\n&quot;, answer );           

    return( 0 );
  }             
</pre>

  <p>This code displays 10, which is 4+3+2+1.</p>

  <p>Here is another example of variable argument function, which is a
  simple printing function:</p>
  <pre class="example-code">
 void my_printf( char *format, ... ) {
   <strong>va_list</strong> argptr;             

   va_start( argptr, format );          

   while( *format != &#39;\0&#39; ) {
     // string
     if( *format == &#39;s&#39; ) {
       char* s = va_arg( argptr, char * );
       printf( &quot;Printing a string: %s\n&quot;, s );
     }
     // character
     else if( *format == &#39;c&#39; ) {
       char c = (char) va_arg( argptr, int );
       printf( &quot;Printing a character: %c\n&quot;, c );
       break;
     }
     // integer
     else if( *format == &#39;d&#39; ) {
       int d = va_arg( argptr, int );
       printf( &quot;Printing an integer: %d\n&quot;, d );
     }          

     *format++;
   }            

   va_end( argptr );
 }              
                

 int main( void ) {             

   my_printf( &quot;sdc&quot;, &quot;This is a string&quot;, 29, &#39;X&#39; );         

   return( 0 );
 }              
</pre>

  <p>This code displays the following output when run:</p>
  <pre class="example-code">
 Printing a string: This is a string
 Printing an integer: 29
 Printing a character: X                
</pre>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>
</body></html>
